import { Injectable } from '@nestjs/common';
import { BaseDao} from '@/common/baseDao/base.service'
import { Util } from '@/common/util/util.service'


@Injectable()
export class AppService {
  constructor
  (
    private readonly baseDao: BaseDao,
    private readonly util:Util
  ) {}

  getHello(): string {
      return 'hello nest';
  }

  async getList():Promise<any>{
    return await this.baseDao.select({
      dbName:"myComplexCollection",
      whereJson:{
        age:this.util._.gte(4).lte(10),
      },
      sortArr:[{name:'age',type:'desc'}],
      pageSize:0,
      pageIndex:1,
    })
  }

  // 插入单条数据
  async add():Promise<any>{
    return await this.baseDao.add({
      dbName:'myComplexCollection',
      dataJson:{
        name:"小明-"+ Math.floor(Math.random()*10+1),
        age:Math.floor(Math.random()*10-1),
        email:"124984@qq.com"
      },
    })
  }

  // 插入多条数据
  async adds():Promise<any>{
    // await this.util.sleep(10000)
    return await this.baseDao.adds({
      dbName:"myComplexCollection",
      dataJson:[{
        name:'yellow',
        age:Math.floor(Math.random()*10+1),
        email:"124941230@qq.com"
      },{
        name:'yellow',
        age:Math.floor(Math.random()*10+1),
        email:"124941230@qq.com"
      }]
    })
  }
  
  // 批量删除
  async del():Promise<any>{
    return await this.baseDao.del({
      dbName:"myComplexCollection",
      whereJson:{
        // age:this._.lt(500000)
      }
    })
  }

  // 批量修改
  async update():Promise<any>{
    return await this.baseDao.update({
      dbName:"myComplexCollection",
      whereJson:{
        // age:this._.gt(0)
      },
      dataJson:{
        name:"小样"
      }
    })
  }

  // 根据ID修改
  async updateById(id:string):Promise<any>{
    console.log("请求结果",1)
    const data =  await this.baseDao.updateById({
      dbName:"myComplexCollection",
      id,
      dataJson:{
        name:'荒凉'
      }
    })
  }

  // 返回更新后的内容
  async updateAndReturn():Promise<any>{
    return await this.baseDao.updateAndReturn({
      dbName:"myComplexCollection",
      whereJson:{
        age:1
      },
      dataJson:{
        name:"日龙天"
      }
    })
  }

  // 根据ID判断存在则替换，不存在则添加
  async setById():Promise<any>{
    return await this.baseDao.setById({
      dbName:"myComplexCollection",
      id:"67219612f88ec33404d79573",
      dataJson:{
        name:"阿萨德",
        email:"5666@qq.com"
      }
    })
  }

  // 根据条件查单条数据
  async findByWhereJson():Promise<any>{
    const {_} = this.util
    return await this.baseDao.select({
      dbName:"myComplexCollection",
      whereJson:{
        age:_.lt(24),
        city:"New York"
      },
      pageSize:0,
      fieldJson:{
        name: true,
        _id:true,
        email:true,
        age:true,
        new_name:"$name"
      }
    })
  }
  
  // 根据条件查询多条数据
  async select():Promise<any>{
    return await this.baseDao.select({
      dbName:"myComplexCollection",
      whereJson:{
        // age:this._.type('number').gte(7)
      },
      sortArr:[{name:'age',type:"asc"}],
      fieldJson:{
        age:true
      }
    })
  }

  // 统计条数
  async count():Promise<any>{
    return await this.baseDao.count({
      dbName:'myComplexCollection',
      whereJson:{
        // age:this._.gte(9)
      }
    })
  }

  async delById():Promise<any>{
    return await this.baseDao.deleteById({
      dbName:"myComplexCollection",
      id:'6720c4f9de3b814de2eee085'
    })
  }
}
